﻿namespace KeRui.Upload
{
    using System;
    using System.Web;

    public class Upload
    {
        private int mCodePage;
        private HttpContext mContext;
        private UploadedFileCollection mFiles;
        private Multipart.Form mForm;
        private bool mOverwrite;

        public Upload()
        {
            Registration.CheckValid();
            try
            {
                this.mContext = HttpContext.Current;
                this.mOverwrite = true;
                this.mCodePage = this.mContext.Request.ContentEncoding.CodePage;
                this.mForm = (Multipart.Form) this.mContext.Items[Multipart.Form.KeyName];
                this.mFiles = new UploadedFileCollection();
                if (this.mForm == null)
                {
                    foreach (string str in this.mContext.ApplicationInstance.Request.Files.Keys)
                    {
                        UploadedFile file = new UploadedFile(this, this.mContext.ApplicationInstance.Request.Files[str]);
                        this.mFiles.AddFile(str, file);
                    }
                }
                else
                {
                    for (int i = 0; i < this.mForm.Count; i++)
                    {
                        if (this.mForm[i].IsFileField)
                        {
                            UploadedFile file2 = new UploadedFile(this, this.mForm[i]);
                            this.mFiles.AddFile(this.mForm[i].FieldName, file2);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (Settings.Current.Advanced.LogExceptions)
                {
                    Log.ExceptionDetails(exception);
                }
                throw exception;
            }
        }

        public void Dispose()
        {
            try
            {
                if (this.mForm != null)
                {
                    this.mForm.Clear();
                }
            }
            catch (Exception exception)
            {
                if (Settings.Current.Advanced.LogExceptions)
                {
                    Log.ExceptionDetails(exception);
                }
                throw exception;
            }
        }

        internal string ResolvePath(string path)
        {
            bool flag = path.IndexOf(':') < 0;
            bool flag2 = path.IndexOf('\\') < 0;
            bool flag3 = (path[0] != '/') || (path[1] != '/');
            if ((flag && flag2) && flag3)
            {
                try
                {
                    string str = Utilities.FindURLFileName(path);
                    path = path.Substring(0, path.Length - str.Length);
                    path = this.Context.ApplicationInstance.Server.MapPath(path);
                    path = path + @"\" + str;
                }
                catch (Exception exception)
                {
                    throw new Exception("Unable to map virtual to physical pathname.", exception);
                }
            }
            if (Utilities.FindFileName(path) == "")
            {
                throw new Exception("No file name provided.");
            }
            return path;
        }

        public void Save(string path)
        {
            try
            {
                path = this.ResolvePath(path);
                FileUtils.WriteDataToFile(path, this.mOverwrite, this.mContext.ApplicationInstance.Request.InputStream);
            }
            catch (Exception exception)
            {
                if (Settings.Current.Advanced.LogExceptions)
                {
                    Log.ExceptionDetails(exception);
                }
                throw exception;
            }
        }

        public int CodePage
        {
            get
            {
                int mCodePage;
                try
                {
                    mCodePage = this.mCodePage;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mCodePage;
            }
            set
            {
                try
                {
                    this.mCodePage = value;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
            }
        }

        public HttpContext Context
        {
            get
            {
                HttpContext mContext;
                try
                {
                    mContext = this.mContext;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mContext;
            }
        }

        public UploadedFileCollection Files
        {
            get
            {
                UploadedFileCollection mFiles;
                try
                {
                    mFiles = this.mFiles;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mFiles;
            }
        }

        public bool Overwrite
        {
            get
            {
                bool mOverwrite;
                try
                {
                    mOverwrite = this.mOverwrite;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mOverwrite;
            }
            set
            {
                try
                {
                    this.mOverwrite = value;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
            }
        }
    }
}

